package com.ruoyi.web.controller.comlbs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.domain.tree.TreeHelper;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OrderConfiguration;
import com.ruoyi.system.domain.vo.order.OrderConfig;
import com.ruoyi.system.service.IOrderConfigurationService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指令配置Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/orderConfiguration")
public class OrderConfigurationController extends BaseController {

    @Autowired
    private IOrderConfigurationService orderConfigurationService;

    @ApiOperation("指令配置树结构")
    @GetMapping("/tree")
    public AjaxResult tree() {
        List<OrderConfiguration> list = orderConfigurationService.selectList(new OrderConfiguration());
        return success(TreeHelper.buildTree(list));
    }

    /**
     * 获取指令配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderConfigurationService.getById(id));
    }

    @ApiOperation("新增指令")
    @ApiOperationSupport(includeParameters = {"orderName","orderType","response","isNormal", "parentId"})
    @Log(title = "指令配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderConfiguration orderConfiguration) {
        return toAjax(orderConfigurationService.save(orderConfiguration));
    }

    @ApiOperation("编辑指令")
    @ApiOperationSupport(includeParameters = {"id","orderName","orderType","response","isNormal", "parentId", "confValue"})
    @Log(title = "指令配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderConfiguration orderConfiguration) {
        return toAjax(orderConfigurationService.updateById(orderConfiguration));
    }

    @ApiOperation("删除指令")
    @Log(title = "指令配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderConfigurationService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("获取指令配置数据")
    @GetMapping("/getConfigValue")
    public AjaxResult getConfigValue(String orderName) {
        return success(orderConfigurationService.getOne(
                new QueryWrapper<OrderConfiguration>().like("order_name", orderName)));
    }
}
